using System;
using System.ComponentModel;
using Castle.DynamicProxy;
using Castle.Windsor;
using FluentValidation;

namespace TimeKeeper.Infrastructure
{
    class EntityFactory : IEntityFactory
    {
        private readonly IWindsorContainer _container;
        private readonly ProxyGenerator _generator = new ProxyGenerator();

        public EntityFactory(IWindsorContainer container)
        {
            _container = container;
        }

        public T Create<T>() where T: class, new()
        {
            return (T) Create(typeof (T));
        }

        public object Create(Type type)
        {
            var validator = (IValidator)_container.Resolve(typeof(IValidator<>).MakeGenericType(type));
            return _generator.CreateClassProxy(type, new[] { typeof(INotifyPropertyChanged), typeof(IDataErrorInfo) }, new DataErrorInfoInterceptor(validator));
        }
    }
}